# Create your views here.

from django.db.models import Q
from django.shortcuts import render_to_response
from sports.models import Game, Location
from sports.forms import ScheduleGameForm
from django.core.context_processors import csrf
from django.template import RequestContext

def search(request):
    query = request.GET.get('q', '')
    if query:
        qset = (
            Q(sport = query)|
            Q(organizer = query)
        )
        results = Game.objects.filter(qset)
    else:
        results = []
    return render_to_response("sports/search.html", {
        "results": results,
        "query": query
    })
    
def organize(request):
    if request.method == 'POST':
        form = ScheduleGameForm(request.POST)
        if form.is_valid():
            #sport = form.cleaned_data['sport']
            form.save()
            
        
    
   # if form.is_valid:
    #    form.save()
        # Process form data
#        sport = form.sport
#        organizer = form.organizer
#        date = form.date
    else:
        form = ScheduleGameForm()
    

    return render_to_response('sports/organize.html', {'form': form}, context_instance=RequestContext(request))    